using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;

namespace Sunny.UI;

public static class StringEx
{
	public const string DefaultCryptString = "!@#$%^&*";

	public static string DealPath(this string dir)
	{
		if (!dir.IsNullOrEmpty())
		{
			if (dir[dir.Length - 1] != Path.DirectorySeparatorChar)
			{
				return dir + Path.DirectorySeparatorChar;
			}
			return dir;
		}
		return dir;
	}

	public static string FormatString(this int decimalPlaces)
	{
		if (decimalPlaces <= 0)
		{
			return "0";
		}
		return "0." + "0".Repeat(decimalPlaces);
	}

	public static bool ToBoolean(this string s, bool def = false)
	{
		if (!bool.TryParse(s, out var result))
		{
			return def;
		}
		return result;
	}

	public static char ToChar(this string s, char def = '\0')
	{
		if (!char.TryParse(s, out var result))
		{
			return def;
		}
		return result;
	}

	public static decimal ToDecimal(this string s, decimal def = 0m)
	{
		if (!decimal.TryParse(s, out var result))
		{
			return def;
		}
		return result;
	}

	public static double ToDouble(this string s, double def = 0.0)
	{
		if (!double.TryParse(s, out var result))
		{
			return def;
		}
		return result;
	}

	public static float ToFloat(this string s, float def = 0f)
	{
		if (!float.TryParse(s, out var result))
		{
			return def;
		}
		return result;
	}

	public static byte ToByte(this string s, byte def = 0)
	{
		if (!byte.TryParse(s, out var result))
		{
			return def;
		}
		return result;
	}

	public static sbyte ToSByte(this string s, sbyte def = 0)
	{
		if (!sbyte.TryParse(s, out var result))
		{
			return def;
		}
		return result;
	}

	public static short ToShort(this string s, short def = 0)
	{
		if (!short.TryParse(s, out var result))
		{
			return def;
		}
		return result;
	}

	public static ushort ToUShort(this string s, ushort def = 0)
	{
		if (!ushort.TryParse(s, out var result))
		{
			return def;
		}
		return result;
	}

	public static int ToInt(this string s, int def = 0)
	{
		if (!int.TryParse(s, out var result))
		{
			return def;
		}
		return result;
	}

	public static uint ToUInt(this string s, uint def = 0u)
	{
		if (!uint.TryParse(s, out var result))
		{
			return def;
		}
		return result;
	}

	public static long ToLong(this string s, long def = 0L)
	{
		if (!long.TryParse(s, out var result))
		{
			return def;
		}
		return result;
	}

	public static ulong ToULong(this string s, ulong def = 0uL)
	{
		if (!ulong.TryParse(s, out var result))
		{
			return def;
		}
		return result;
	}

	public static DateTime ToDateTime(this string s, DateTime def = default(DateTime))
	{
		if (!DateTime.TryParse(s, out var result))
		{
			return def;
		}
		return result;
	}

	public static DateTime ToDateTime(this string s, string formatString)
	{
		return DateTime.ParseExact(s, formatString, CultureInfo.InvariantCulture);
	}

	public static DateTime ToDateTimeEx(this string s, string formatString, DateTime def = default(DateTime))
	{
		int year = def.Year;
		int month = def.Month;
		int day = def.Day;
		int hour = def.Hour;
		int minute = def.Minute;
		int second = def.Second;
		int millisecond = def.Millisecond;
		if (formatString.Contains("yyyy"))
		{
			year = s.Substring(formatString.IndexOf("yyyy", StringComparison.CurrentCulture), 4).ToInt();
		}
		else if (formatString.Contains("yy"))
		{
			year = 2000 + s.Substring(formatString.IndexOf("yy", StringComparison.CurrentCulture), 2).ToInt();
		}
		if (formatString.Contains("MM"))
		{
			month = s.Substring(formatString.IndexOf("MM", StringComparison.CurrentCulture), 2).ToInt();
		}
		if (formatString.Contains("dd"))
		{
			day = s.Substring(formatString.IndexOf("dd", StringComparison.CurrentCulture), 2).ToInt();
		}
		if (formatString.Contains("HH"))
		{
			hour = s.Substring(formatString.IndexOf("HH", StringComparison.CurrentCulture), 2).ToInt();
		}
		if (formatString.Contains("mm"))
		{
			minute = s.Substring(formatString.IndexOf("mm", StringComparison.CurrentCulture), 2).ToInt();
		}
		if (formatString.Contains("ss"))
		{
			second = s.Substring(formatString.IndexOf("ss", StringComparison.CurrentCulture), 2).ToInt();
		}
		if (formatString.Contains("fff"))
		{
			millisecond = s.Substring(formatString.IndexOf("fff", StringComparison.CurrentCulture), 3).ToInt();
		}
		try
		{
			return new DateTime(year, month, day, hour, minute, second, millisecond);
		}
		catch
		{
			return def;
		}
	}

	public static Guid ToGuid(this string s, Guid def = default(Guid))
	{
		if (!Guid.TryParse(s, out var result))
		{
			return def;
		}
		return result;
	}

	public static bool IsBoolean(this string s)
	{
		bool result;
		return bool.TryParse(s, out result);
	}

	public static bool IsChar(this string s)
	{
		char result;
		return char.TryParse(s, out result);
	}

	public static bool IsDecimal(this string s)
	{
		decimal result;
		return decimal.TryParse(s, out result);
	}

	public static bool IsDouble(this string s)
	{
		double result;
		return double.TryParse(s, out result);
	}

	public static bool IsFloat(this string s)
	{
		float result;
		return float.TryParse(s, out result);
	}

	public static bool IsByte(this string s)
	{
		byte result;
		return byte.TryParse(s, out result);
	}

	public static bool IsSByte(this string s)
	{
		sbyte result;
		return sbyte.TryParse(s, out result);
	}

	public static bool IsShort(this string s)
	{
		short result;
		return short.TryParse(s, out result);
	}

	public static bool IsUShort(this string s)
	{
		ushort result;
		return ushort.TryParse(s, out result);
	}

	public static bool IsInt(this string s)
	{
		int result;
		return int.TryParse(s, out result);
	}

	public static bool IsUInt(this string s)
	{
		uint result;
		return uint.TryParse(s, out result);
	}

	public static bool IsLong(this string s)
	{
		long result;
		return long.TryParse(s, out result);
	}

	public static bool IsULong(this string s)
	{
		ulong result;
		return ulong.TryParse(s, out result);
	}

	public static bool IsDateTime(this string s)
	{
		DateTime result;
		return DateTime.TryParse(s, out result);
	}

	public static bool IsGuid(this string s)
	{
		Guid result;
		return Guid.TryParse(s, out result);
	}

	public static string Repeat(this string str, int count)
	{
		if (count <= 0)
		{
			return string.Empty;
		}
		StringBuilder stringBuilder = new StringBuilder();
		stringBuilder.Append("");
		for (int i = 0; i < count; i++)
		{
			stringBuilder.Append(str);
		}
		return stringBuilder.ToString();
	}

	public static string Left(this string s, int length)
	{
		if (!s.IsNullOrEmpty())
		{
			if (length > s.Length)
			{
				return s;
			}
			return s.Substring(0, length);
		}
		return s;
	}

	public static string Middle(this string s, int start, int length)
	{
		if (!s.IsNullOrEmpty())
		{
			if (length > s.Length - start)
			{
				return s.Substring(start, s.Length - start);
			}
			return s.Substring(start, length);
		}
		return s;
	}

	public static bool IsNullOrEmpty(this string s)
	{
		return string.IsNullOrEmpty(s);
	}

	public static bool IsValid(this string s)
	{
		return !string.IsNullOrEmpty(s);
	}

	public static string TrimToMaxLength(this string value, int maxLength)
	{
		if (value != null && value.Length > maxLength)
		{
			return value.Substring(0, maxLength);
		}
		return value;
	}

	public static bool IsNullOrWhiteSpace(this string s)
	{
		return string.IsNullOrWhiteSpace(s);
	}

	public static string Right(this string s, int length)
	{
		if (s.IsNullOrEmpty())
		{
			return s;
		}
		if (length < s.Length)
		{
			return s.Substring(s.Length - length, length);
		}
		return s;
	}

	public static bool IsNumber(this string s)
	{
		if (s.IsNullOrEmpty())
		{
			return false;
		}
		return new Regex("^[0-9]+$").Match(s).Success;
	}

	public static bool IsNumberWithSign(this string s)
	{
		if (s.IsNullOrEmpty())
		{
			return false;
		}
		return new Regex("^[+-]?[0-9]+$").Match(s).Success;
	}

	public static bool HasCnChar(this string s)
	{
		if (s.IsNullOrEmpty())
		{
			return false;
		}
		return new Regex("[一-龥]").Match(s).Success;
	}

	public static string Md5String(this string s)
	{
		return s.Md5String(Encoding.UTF8).ToUpper();
	}

	public static string Md5String(this string s, Encoding encoding)
	{
		if (s.IsNullOrEmpty())
		{
			throw new ArgumentNullException("s");
		}
		if (encoding == null)
		{
			throw new ArgumentNullException("encoding");
		}
		using MD5 mD = MD5.Create();
		return mD.ComputeHash(encoding.GetBytes(s)).ToHexString().ToUpper();
	}

	public static string DesEncrypt(this string s, string key = "")
	{
		if (s.IsNullOrEmpty())
		{
			return s;
		}
		try
		{
			string s2 = key + "!@#$%^&*";
			byte[] array = Encoding.UTF8.GetBytes(s2).BlockCopy(0, 8);
			byte[] rgbIV = array;
			byte[] bytes = Encoding.UTF8.GetBytes(s);
			using DESCryptoServiceProvider dESCryptoServiceProvider = new DESCryptoServiceProvider();
			using MemoryStream memoryStream = new MemoryStream();
			using CryptoStream cryptoStream = new CryptoStream(memoryStream, dESCryptoServiceProvider.CreateEncryptor(array, rgbIV), CryptoStreamMode.Write);
			cryptoStream.Write(bytes, 0, bytes.Length);
			cryptoStream.FlushFinalBlock();
			return Convert.ToBase64String(memoryStream.ToArray());
		}
		catch
		{
			return "";
		}
	}

	public static string DesDecrypt(this string s, string key = "")
	{
		if (s.IsNullOrEmpty())
		{
			return s;
		}
		try
		{
			string s2 = key + "!@#$%^&*";
			byte[] array = Encoding.UTF8.GetBytes(s2).BlockCopy(0, 8);
			byte[] rgbIV = array;
			byte[] array2 = Convert.FromBase64String(s);
			using DESCryptoServiceProvider dESCryptoServiceProvider = new DESCryptoServiceProvider();
			using MemoryStream memoryStream = new MemoryStream();
			using CryptoStream cryptoStream = new CryptoStream(memoryStream, dESCryptoServiceProvider.CreateDecryptor(array, rgbIV), CryptoStreamMode.Write);
			cryptoStream.Write(array2, 0, array2.Length);
			cryptoStream.FlushFinalBlock();
			return Encoding.UTF8.GetString(memoryStream.ToArray());
		}
		catch
		{
			return "";
		}
	}

	public static bool IsIP4(this string s)
	{
		if (string.IsNullOrEmpty(s))
		{
			return false;
		}
		string[] array = s.Split(".");
		if (array.Length != 4)
		{
			return false;
		}
		string[] array2 = array;
		foreach (string text in array2)
		{
			if (!text.Trim().IsNumber())
			{
				return false;
			}
			if (text.Trim().ToInt() < 0)
			{
				return false;
			}
			if (text.Trim().ToInt() > 255)
			{
				return false;
			}
		}
		return true;
	}

	public static string[] Split(this string s, string separator, bool ignoreEmpty = false)
	{
		if (s.IsNullOrEmpty())
		{
			return null;
		}
		string[] array = s.Split(new string[1] { separator }, StringSplitOptions.None);
		if (!ignoreEmpty)
		{
			return array;
		}
		List<string> list = new List<string>();
		string[] array2 = array;
		foreach (string text in array2)
		{
			if (!text.IsNullOrEmpty())
			{
				list.Add(text);
			}
		}
		return list.ToArray();
	}

	public static int SplitSeparatorCount(this string s, string separator)
	{
		if (s.IsNullOrEmpty())
		{
			return 0;
		}
		if (!s.Contains(separator))
		{
			return 0;
		}
		string text = s.Replace(separator, "");
		return (s.Length - text.Length) / separator.Length;
	}

	public static string SplitLast(this string s, string separator, bool ignoreEmpty = false)
	{
		if (s.IsNullOrEmpty())
		{
			return s;
		}
		string[] array = s.Split(separator, ignoreEmpty);
		if (array.Length != 0)
		{
			return array[array.Length - 1];
		}
		return string.Empty;
	}

	public static string SplitFirst(this string s, string separator, bool ignoreEmpty = false)
	{
		if (s.IsNullOrEmpty())
		{
			return s;
		}
		string[] array = s.Split(separator, ignoreEmpty);
		if (array.Length != 0)
		{
			return array[0];
		}
		return string.Empty;
	}

	public static string SplitIndex(this string s, string separator, int index, bool ignoreEmpty = false)
	{
		if (s.IsNullOrEmpty())
		{
			return s;
		}
		string[] array = s.Split(separator, ignoreEmpty);
		if (array.Length != 0)
		{
			return array[index];
		}
		return string.Empty;
	}

	public static string SplitBeforeLast(this string s, string separator)
	{
		if (s.IsNullOrEmpty())
		{
			return s;
		}
		int num = s.LastIndexOf(separator, StringComparison.Ordinal);
		if (num <= 0)
		{
			return s;
		}
		return s.Substring(0, num);
	}

	public static bool IsMail(this string s)
	{
		if (s.IsNullOrEmpty())
		{
			return false;
		}
		string text = s.Trim();
		if (text == "" || text.Length == 0)
		{
			return false;
		}
		if (s.SplitSeparatorCount("@") != 1)
		{
			return false;
		}
		string[] array = text.Split('@');
		if (array.Length != 2)
		{
			return false;
		}
		if (array[0] == "" || array[1] == "")
		{
			return false;
		}
		string[] array2 = array;
		foreach (string text2 in array2)
		{
			for (int j = 0; j < text2.Length; j++)
			{
				string separator = text2.Substring(j, 1).ToLower();
				if ("abcdefghijklmnopqrstuvwxyz_-.0123456789".SplitSeparatorCount(separator) == 0)
				{
					return false;
				}
			}
		}
		return true;
	}

	public static byte[] ToEnBytes(this string s, int len)
	{
		byte[] array = new byte[len];
		if (s.IsNullOrEmpty())
		{
			return array;
		}
		byte[] array2 = s.ToEncodeBytes(Encoding.ASCII);
		Array.Copy(array2, 0, array, 0, Math.Min(array2.Length, len));
		return array;
	}

	public static string ToEnString(this byte[] s)
	{
		if (s == null)
		{
			return "";
		}
		if (s.Length == 0)
		{
			return "";
		}
		return s.ToEncodeString(Encoding.ASCII).Trim(default(char)).Trim();
	}

	public static byte[] ToEncodeBytes(this string dealString, Encoding encoding)
	{
		if (dealString.IsNullOrEmpty())
		{
			return null;
		}
		return encoding.GetBytes(dealString);
	}

	public static string ToEncodeString(this byte[] dealBytes, Encoding encoding)
	{
		if (dealBytes == null)
		{
			return null;
		}
		if (dealBytes.Length == 0)
		{
			return null;
		}
		return encoding.GetString(dealBytes);
	}

	public static string Between(this string value, string x, string y)
	{
		if (value.IsNullOrEmpty())
		{
			return value;
		}
		int num = value.IndexOf(x, StringComparison.Ordinal);
		int num2 = value.LastIndexOf(y, StringComparison.Ordinal);
		if (num == -1 || num == -1)
		{
			return string.Empty;
		}
		int num3 = num + x.Length;
		if (num3 < num2)
		{
			return value.Substring(num3, num2 - num3).Trim();
		}
		return string.Empty;
	}

	public static string Base64Encode(this string value)
	{
		return value.Base64Encode(null);
	}

	public static string Base64Encode(this string value, Encoding encoding)
	{
		if (value.IsNullOrEmpty())
		{
			return value;
		}
		encoding = encoding ?? Encoding.UTF8;
		return Convert.ToBase64String(encoding.GetBytes(value));
	}

	public static string Base64Decode(this string encodedValue)
	{
		return encodedValue.Base64Decode(null);
	}

	public static string Base64Decode(this string encodedValue, Encoding encoding)
	{
		if (encodedValue.IsNullOrEmpty())
		{
			return encodedValue;
		}
		encoding = encoding ?? Encoding.UTF8;
		byte[] bytes = Convert.FromBase64String(encodedValue);
		return encoding.GetString(bytes);
	}

	public static string SHA1Hash(this string stringToHash)
	{
		if (stringToHash.IsNullOrEmpty())
		{
			return stringToHash;
		}
		byte[] bytes = Encoding.UTF8.GetBytes(stringToHash);
		using SHA1CryptoServiceProvider sHA1CryptoServiceProvider = new SHA1CryptoServiceProvider();
		return Convert.ToBase64String(sHA1CryptoServiceProvider.ComputeHash(bytes));
	}

	public static string RemoveLeft(this string str, int number_of_characters)
	{
		if (str.IsNullOrEmpty())
		{
			return str;
		}
		if (str.Length <= number_of_characters)
		{
			return "";
		}
		return str.Substring(number_of_characters);
	}

	public static string RemoveRight(this string str, int number_of_characters)
	{
		if (str.IsNullOrEmpty())
		{
			return str;
		}
		if (str.Length <= number_of_characters)
		{
			return "";
		}
		return str.Substring(0, str.Length - number_of_characters);
	}

	public static string GZipCompress(this string input)
	{
		if (!input.IsNullOrEmpty())
		{
			return Convert.ToBase64String(Encoding.Default.GetBytes(input).GZipCompress());
		}
		return input;
	}

	public static string GZipDecompress(this string input)
	{
		if (!input.IsNullOrEmpty())
		{
			return Encoding.Default.GetString(Convert.FromBase64String(input).GZipDecompress());
		}
		return input;
	}

	public static bool IsPureEnglishChar(string str)
	{
		if (str.IsNullOrEmpty())
		{
			return false;
		}
		return new Regex("^[a-zA-Z]+$").IsMatch(str);
	}

	public static bool IsNameSpace(string str)
	{
		if (str.IsNullOrEmpty())
		{
			return false;
		}
		if (new Regex("^[a-zA-Z.]+$").IsMatch(str) && !str.StartsWith(".") && !str.EndsWith(".") && str.SplitSeparatorCount(".") <= 3 && !str.Contains(".."))
		{
			return !str.Contains("...");
		}
		return false;
	}

	public static string ToSBC(this string input)
	{
		if (input.IsNullOrEmpty())
		{
			return input;
		}
		char[] array = input.ToCharArray();
		for (int i = 0; i < array.Length; i++)
		{
			if (array[i] == ' ')
			{
				array[i] = '\u3000';
			}
			else if (array[i] < '\u007f')
			{
				array[i] = (char)(array[i] + 65248);
			}
		}
		return new string(array);
	}

	public static string ToDBC(this string input)
	{
		if (input.IsNullOrEmpty())
		{
			return input;
		}
		char[] array = input.ToCharArray();
		for (int i = 0; i < array.Length; i++)
		{
			if (array[i] == '\u3000')
			{
				array[i] = ' ';
			}
			else if (array[i] > '\uff00' && array[i] < '｟')
			{
				array[i] = (char)(array[i] - 65248);
			}
		}
		return new string(array);
	}
}
